from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import json
from bs4 import BeautifulSoup
import urllib


def buildingParameters(data):

    # print(data)
    #
    typeCorr = {
        "TI": "acmdlTitle:",
        "AU": "persons.authors.personName:",
        "AB:":"recordAbstract:",

    }
    logCorr = {
        "and": "AND",
        "or": "OR",
        "not": "NOT",
        "###": "###"
    }

    data["log"].append("###")

    tempStr = []
    for i in range(len(data["values"])):

        try:
            # strr = data["values"][i] + "[{}]".format(typeCorr[data["type"][i]]) + ' {} '.format(logCorr[data["log"][i]])
            strr = typeCorr[data["type"][i]] + "(" + data["values"][i] + ") {} ".format(logCorr[data["log"][i]])
            tempStr.append(strr)
        except:
            raise



    if len(tempStr) == 0:
        return -1

    strr = ''
    for value in tempStr:
        strr += value


    return strr.strip().strip("#").strip()

    # return strr.strip().strip("|").strip()

def getCookies():

    headers = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    }

    params = (
        ('dbname', 'WorldCatDissertations'),
        ('done', 'referer'),
    )
    proxies = {
        "https": "https://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    response = s.get('http://firstsearch.oclc.org/fsip', headers=headers, params=params,proxies=proxies)
    c = requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取

    cookies = s.cookies.get_dict()
    return cookies

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select("table.mainPanel > tbody > tr > td table")[10].select("tbody > tr")


    for li in lis:

        tempData = resultData()

        try:
            tempData["titleName"] = li.select("td")[2].select("a")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = "http://firstsearch.oclc.org" + li.select("td")[2].select("a")[0]["href"]
        except:
            pass

        try:
            tempData["source"] = li.select("it")[0].text.strip()
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword,cookies):


    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Origin': 'https://firstsearch.oclc.org',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'https://firstsearch.oclc.org/WebZ/FSPrefs?entityjsdetect=:javascript=true:screensize=large:sessionid=fsapp4-37147-jrhcianf-pxkwts:entitypagenum=1:0',

        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en-CA;q=0.7,en;q=0.6',
    }

    data = {
        'quickmenu': '',
        'dbname': 'WorldCatDissertations',
        'term1': keyword,
        'index1': 'kw:',
        'operator2': 'and',
        'term2': '',
        'index2': 'kw:',
        'operator3': 'and',
        'term3': '',
        'index3': 'kw:',
        'limit-yr:': '',
        'limit-li:': '',
        'rank': '',
        'action': '\u68C0\u7D22'
    }
    proxies = {
        "https": "https://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.post('https://firstsearch.oclc.org/WebZ/FSQUERY?format=BI:next=html/records.html:bad=html/records.html:numrecs=10:sessionid={}:entitypagenum=4:0:searchtype=advanced'.format(cookies["sess"]),headers=headers, data=data, proxies=proxies)
    response.encoding = 'utf-8'
    # print(response.text)

    allData = formData(response)


    return allData


def index(request):
    data = json.loads(request.POST.get('data', '1'))

    cookies = getCookies()
    allData = getData(data["values"][0],cookies)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")